<?php
require_once("Response.php");
class Question{
	public $link;
	public $response;
	public function __construct(){
		$this->response = new Response();
		$this->link = create_connection();
	}
	

	public function get(){
		$innersql = "select id from questions";
		$condition = "";
		// var_dump($_POST['data']);
		$sort = null;
		if(isset($_POST['data'])){
			$data = $_POST['data'];
			$page = intval(filter_sql($data['page']));
			$limit = intval(filter_sql($data['limit']));
			$title = filter_sql($data['title']);
			$type = filter_sql($data['type']);
			$sort = filter_sql($data['sort']);
			$inner = [];
			if($type) $inner[] = " qtypeid = $type";
			if($title != null) $inner[] = " title like '%$title%'";
			$arrlength=count($inner);
			if($arrlength) $condition .= ' where';
			// var_dump($inner);
			for($x=0;$x<$arrlength-1;$x++)
				$condition .= $inner[$x] . ' and';
			if($arrlength>=1) $condition .= $inner[$arrlength-1];
			$condition .= " order by id";
			if($sort == '-id') $condition .= " desc";
			$innersql .= $condition;
			if($page && $limit) $innersql = $innersql . " limit ".($limit * ($page - 1)).",$limit";
		}else{
			$innersql .= " order by id limit 0,20 ";
		}
		// echo $innersql;
		$sql = "select count(id) as total from questions
		" . $condition;
		$result=@execute_sql($this->link,'zhihu',$sql);
		if($result) {
			$total = intval(mysqli_fetch_assoc($result)['total']);
			$sql = "select q.id as id,qtypeid as qtype_id,users_id as user_id,
			       username,title,agree,addtime,look,q.status 
			       from questions q
			       inner join ($innersql) as temp_table using(id)
			       left join users u on q.users_id = u.id
			       order by id";
			if($sort == '-id') $sql .= " desc";
			// echo $sql;
			$result=@execute_sql($this->link,'zhihu',$sql);
			if($result){
				$data = array();//初始化数组;
			    while ($row = mysqli_fetch_assoc($result)) {
			    	$alter = new QuestionData();//实例化对象;
					$alter->id = $row['id'];
					$alter->user_id = $row['user_id'];
					$alter->username = $row['username'];
					$alter->qtype_id = $row['qtype_id'];
					$alter->title = $row['title'];
					$alter->addtime = $row['addtime'];
					$alter->look = $row['look'];
					$alter->agree = $row['agree'];
					$alter->status = $row['status'];
					$data[] = $alter;
				}
				$this->response->returnData(200,'success',["items"=>$data,"total"=>$total]);
			}else {
				$this->response->returnError();
			}
		} else {
			$this->response->returnError();
		}
	}

	public function delete(){
		if(isset($_POST['data'])){
			$data = $_POST['data'];
			$id = filter_sql($data['id']);
			$sql = "delete from questions where id=$id";
			// echo $sql;
			if($result = @execute_sql($this->link,'zhihu',$sql)){
				$this->response->returnData(200,'success',null);
			}else{
				$this->response->returnError();
			}
		}else {
			$this->response->returnError();
		}
	}

	public function prohibit(){
		if(isset($_POST['data'])){
			$data = $_POST['data'];
			$id = filter_sql($data['id']);
			$status = filter_sql($data['status']);
			$sql = "update questions set status='$status' where id=$id";
			// echo $sql;
			if($result = @execute_sql($this->link,'zhihu',$sql)){
				$this->response->returnData(200,'success',null);
			}else{
				$this->response->returnError();
			}
		}else {
			$this->response->returnError();
		}
	}
}
class QuestionData
{
	public $id;
	public $qtype_id;
	public $user_id;
	public $username;
	public $title;
	public $agree;
	public $addtime;
	public $look;
	public $status;
}